package com.hxc.orangeyule.fragment;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.hxc.orangeyule.R;
import com.hxc.orangeyule.callback.DataCallBack;
import com.hxc.orangeyule.config.NetConfig;
import com.hxc.orangeyule.modle.RecommendContent;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/14.
 */
public class RecommendFragment extends MyBaseFragment {

    private List<RecommendContent.DataEntity> mDataEntityList;
    private ViewPager viewPager;
    private List<RecommendContent.DataEntity.InfoEntity> mInfoEntities;
    private PagerAdapter mPagerAdapter;
    private ImageView iv;
    private BitmapUtils mBitmapUtils;
    private List<ImageView> mImageViewList;


    @Override
    protected void setListener() {

    }

    @Override
    protected void setData() {
        getDataFragment(NetConfig.RECOMMEND_PATH, new DataCallBack() {
            @Override
            public void onSuccessed(String result) {

                if (TextUtils.isEmpty(result)) {
                    mDataEntityList.clear();
                    mDataEntityList.add(new Gson().fromJson(result, RecommendContent.class).getData());
                    mInfoEntities.clear();
                    mInfoEntities.addAll(new Gson().fromJson(result, RecommendContent.class).getData().getInfo());
                }
            }
        });

        for (int i = 0; i < mInfoEntities.size(); i++) {
            String pic = mInfoEntities.get(i).getPic();
            String title = mInfoEntities.get(i).getTitle();
            iv = new ImageView(getActivity());
            if (!TextUtils.isEmpty(title)) {
                mBitmapUtils.display(iv, title);

            }
            mImageViewList.add(iv);
        }

        mPagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return mImageViewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mImageViewList.get(position));
                return super.instantiateItem(container, position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
             //   super.destroyItem(container, position, object);
            }
        };

    }

    @Override
    protected void initData() {
        mDataEntityList = new ArrayList<>();
        mInfoEntities = new ArrayList<>();
        mBitmapUtils = new BitmapUtils(getActivity());
        mImageViewList = new ArrayList<>();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.recommend_layout, container, false);
        rootView.findViewById(R.id.viewpager_recommend);
        viewPager = (ViewPager) rootView.findViewById(R.id.viewpager_recommend);

        return rootView;
    }

}
